/**
 * 数据处理类，可根据工程配置
 * （Data processing class, can be configured according to the project）
 */
import type {
  AxiosInstance,
  AxiosRequestConfig,
  AxiosResponse,
  InternalAxiosRequestConfig,
} from 'axios';
import type { RequestOptions, Result } from '#/axios';

/**
 * 创建Axios选项
 */
export interface CreateAxiosOptions extends AxiosRequestConfig {
  /** 身份验证方案 */
  authenticationScheme?: string;
  /** Axios转换 */
  transform?: AxiosTransform;
  /** 请求参数 */
  requestOptions?: RequestOptions;
}

/**
 * Axios转换
 */
export abstract class AxiosTransform {
  /**
   * A function that is called before a request is sent. It can modify the request configuration as needed.
   * 在发送请求之前调用的函数。它可以根据需要修改请求配置。
   */
  beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig;
  /**
   * @description: 处理响应数据
   */
  transformResponseHook?: (res: AxiosResponse<Result>, options: RequestOptions) => any;
  /**
   * @description: 请求失败处理
   */
  requestCatchHook?: (e: Error, options: RequestOptions) => Promise<any>;

  /**
   * @description: 请求拦截器
   */
  requestInterceptors?: (
    config: InternalAxiosRequestConfig,
    options: CreateAxiosOptions,
  ) => InternalAxiosRequestConfig;
  /**
   * @description: 请求拦截器错误处理
   */
  requestInterceptorsCatch?: (error: Error) => void;

  /**
   * @description: 响应结果拦截器
   */
  responseInterceptors?: (
    axiosInstance: AxiosInstance,
    res: AxiosResponse<any>,
  ) => Promise<AxiosResponse<any>> | AxiosResponse<any>;
  /**
   * @description: 响应结果拦截器错误处理
   */
  responseInterceptorsCatch?: (axiosInstance: AxiosInstance, error: Error) => void;
}
